﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Web_Oto.Bases;

namespace Web_Oto.Data
{
    public class ConsultServiceDal : dalObject
    {
        #region constructors

        public ConsultServiceDal(string connectionstring, string connectiontype)
            : base(connectionstring, connectiontype)
        {

        }

        public ConsultServiceDal(IDbConnection sqlConnection, string connectiontype, SqlTransaction dbTransaction)
            : base(sqlConnection, connectiontype, dbTransaction)
        {

        }

        #endregion

        #region methods

        public IList<ConsultService> GetAllConsultService(string name = "", string intro= "")
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.ConsultServices.Where(p => (p.Name.Contains(name) || string.IsNullOrEmpty(name)) 
                    && (p.Intro.Contains(intro) || string.IsNullOrEmpty(intro))).ToList();
            }
        }

        public IList<ConsultService> GetAllConsultService(int? currentPage, int itemPerPage, out int totalRecord)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var query = db.ConsultServices.ToList();
                totalRecord = query.Count;
                return query.Skip((Convert.ToInt32(currentPage) - 1) * itemPerPage)
                    .Take(Convert.ToInt32(currentPage) * itemPerPage).ToList();
            }
        }

        public IList<ConsultService> GetTopServices(int countItems)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.ConsultServices.Take(countItems).OrderBy(c => c.Date).ToList();
            }
        }

        public IList<ConsultService> GetAnotherConsultService(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.ConsultServices.Where(c => c.Id != id).OrderBy(c => c.Id).Take(5).ToList();
            }
        }

        public ConsultService GetNewsById(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.ConsultServices.FirstOrDefault(c => c.Id == id);
            }
        }

        public int Delete(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var consultService = db.ConsultServices.FirstOrDefault(p => p.Id == id);
                    if (consultService != null)
                    {
                        db.ConsultServices.DeleteOnSubmit(consultService);
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public int Update(ConsultService consultService)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var consultServiceDb = db.ConsultServices.FirstOrDefault(p => p.Id == consultService.Id);
                    if (consultServiceDb != null)
                    {
                        consultServiceDb.Name = consultService.Name;
                        consultServiceDb.Description = consultService.Description;
                        consultServiceDb.IsHot = consultService.IsHot;
                        consultServiceDb.IsHomePage = consultService.IsHomePage;
                        consultServiceDb.Date = consultService.Date;
                        if (!string.IsNullOrEmpty(consultService.Image))
                            consultServiceDb.Image = consultService.Image;
                        consultServiceDb.Intro = consultService.Intro;
                        consultServiceDb.TitleOfImage = consultService.TitleOfImage;
                        consultServiceDb.SEO_Title = consultService.SEO_Title;
                        consultServiceDb.SEO_Keyword = consultService.SEO_Keyword;
                        consultServiceDb.SEO_Description = consultService.SEO_Description;
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public int Insert(ConsultService consultService)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    db.ConsultServices.InsertOnSubmit(consultService);
                    db.SubmitChanges();
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }

        public string GetFilePathById(int id)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var consultService = db.ConsultServices.FirstOrDefault(c => c.Id == id);
                if (consultService != null)
                    return consultService.Image;
            }

            return string.Empty;
        }

        #endregion
    }
}
